<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useLoginStore } from '@/store/login'
import { ElMessage } from 'element-plus'

const ruleFormRef = ref<any>()

// 表单数据
const form = ref({
  username: 'admin',
  password: 'atguigu123',
})

// 按钮loading效果
const loading = ref(false)

// 定义表单校验规则
const validatorPwd = (rule:any, value:any, callback: Function) => {
  /**
   * rule：校验规则对象
   * value：表单元素文本
   * 函数：符合要求callback放行；不符合则注入错误信息
   */
  if(/^[a-zA-Z0-9-_]{6,}$/.test(value)){
    callback()
  } else {
    callback(new Error('密码格式为至少6位的大小写英文', rule))
  }
}
const rules = ref<any>({
  username: [
    { required: true, message: '请输入账号', trigger: ['blur', 'change'] },
    {
      min: 5,
      max: 10,
      message: '账号长度至少5位',
      trigger: ['blur', 'change'],
    },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
    { min: 6, message: '至少6位', trigger: ['blur', 'change'] },
    {validator: validatorPwd, trigger: ['blur', 'change'] }
  ],
})

const { userLogin } = useLoginStore()

// 点击登录按钮
const onSubmit = (formEl: any | undefined) => {
  if (!formEl) return
  // validate方法也会返回一个promise对象，有成功和失败状态。
  formEl.validate(async (valid: any, fields: any) => {
    if (valid) {
      try {
        loading.value = true
        await userLogin(form.value)
        loading.value = false
      } catch (error) {
        loading.value = false
      }
    } else {
      ElMessage({
        message: fields[Object.keys(fields)[0]][0].message,
        type: 'error'
      })
    }
  })
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <!-- 左侧占位，屏幕宽度小于768时，左侧无宽度 -->
      <el-col :span="12" :xs="0"></el-col>
      <!-- 右侧表单，屏幕宽度小于768时，右侧占满 -->
      <el-col :span="12" :xs="24">
        <el-form ref="ruleFormRef" :rules="rules" :model="form" class="login_form">
          <h1>Hello</h1>
          <h2>欢迎来到刀刀甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="form.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              show-password
              type="password"
              v-model="form.password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              @click="onSubmit(ruleFormRef)"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: #fff;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: #fff;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
